package ds.factcom.view.ui.client;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

import ds.factcom.controller.client.IClientController;
import ds.factcom.domain.client.DocumentType;
import ds.factcom.exception.EmptyStringException;
import ds.factcom.exception.IsNotNumberException;
import ds.factcom.exception.IsNotStringException;
import ds.factcom.exception.NotCorrectDocumentNumberException;
import ds.factcom.exception.NotCorrectDocumentTypeException;
import ds.factcom.exception.NotExistDomainException;
import ds.factcom.view.ui.Messages;
import ds.factcom.view.validate.ValidatorClient;

public class SearchClientView extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5079590152630575195L;
	
	private JLabel documentNumberLabel;
	private JTextField documentNumberTF;
	private JLabel documentTypeLabel;
	private JComboBox documentTypeCB;
	private JButton acceptBtn;
	private JButton cancelBtn;

	private final IClientController iClientController;

	public SearchClientView(IClientController iClientController) {
		this.iClientController = iClientController;
		initGUI();
	}
	
	private void initGUI() {
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setTitle("Buscar Usuario");
		setSize(400, 100);
		setLocation(400, 300);
		setResizable(false);
		setModal(true);
		setLayout(new GridLayout(3,3));
		add(getDocumentTypeLabel());
		add(getDocumentTypeCB());
		add(getDocumentNumberLabel());
		add(getDocumentNumberTF());
		add(getAcceptBtn());
		add(getCancelBtn());
	}

	public JTextField getDocumentNumberTF() {
		if (documentNumberTF == null) {
			documentNumberTF = new JTextField();
		}
		return documentNumberTF;
	}

	public JComboBox getDocumentTypeCB() {
		if (documentTypeCB == null) {
			documentTypeCB = new JComboBox();
			DefaultComboBoxModel comboBoxModel = new DefaultComboBoxModel();
			comboBoxModel.addElement(DocumentType.CUIT);
			comboBoxModel.addElement(DocumentType.DU);
			comboBoxModel.addElement(DocumentType.DNI);
			documentTypeCB.setModel(comboBoxModel);
		}
		return documentTypeCB;
	}

	public JLabel getDocumentNumberLabel() {
		if (documentNumberLabel == null) {
			documentNumberLabel = new JLabel("Numero de documento");
		}
		return documentNumberLabel;
	}

	public JLabel getDocumentTypeLabel() {
		if (documentTypeLabel == null) {
			documentTypeLabel = new JLabel("Tipo de Documento");
		}
		return documentTypeLabel;
	}

	public JButton getAcceptBtn() {
		if (acceptBtn == null) {
			acceptBtn = new JButton("Aceptar");
			acceptBtn.addActionListener(new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent e) {
					try {
						String documentType = getDocumentTypeCB().getModel()
						.getSelectedItem().toString();
						String documentNumber = getDocumentNumberTF().getText();
						ValidatorClient.isCorrectDocumentType(documentType);
						ValidatorClient.isCorrectDocumentNumber(documentNumber);
						iClientController.findByDocument(documentType, documentNumber);
						SearchClientView.this.dispose();
					} catch (NotExistDomainException e1) {
						Messages.showErrorMessage(SearchClientView.this, e1.getMessage());
					} catch (EmptyStringException e1) {
						Messages.showErrorMessage(SearchClientView.this, e1.getMessage());
					} catch (IsNotStringException e1) {
						Messages.showErrorMessage(SearchClientView.this, e1.getMessage());
					} catch (NotCorrectDocumentTypeException e1) {
						Messages.showErrorMessage(SearchClientView.this, e1.getMessage());
					} catch (IsNotNumberException e1) {
						Messages.showErrorMessage(SearchClientView.this, e1.getMessage());
					} catch (NotCorrectDocumentNumberException e1) {
						Messages.showErrorMessage(SearchClientView.this, e1.getMessage());
					}
				}
			});
		}
		return acceptBtn;
	}

	public JButton getCancelBtn() {
		if (cancelBtn == null) {
			cancelBtn = new JButton("Cancelar");
			cancelBtn.addActionListener(new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent e) {
					getDocumentNumberTF().setText("");
				}
			});
		}
		return cancelBtn;
	}

	public void open() {
		setVisible(true);
	}
}